import java.util.concurrent.TimeUnit;

/**
 * 保证可见性的例子
 */
public class Visibility {
    // 不加就会死循环
    private static volatile int sum = 0;
    public static void main(String[] args) throws Exception {
        new Thread(()-> {
            while (sum == 0) {
                
            }
        }).start();
        TimeUnit.SECONDS.sleep(1);
        sum = 1;
        System.out.println("sum = " + sum);   
    }
}
